package com.hsd.mapper;

import com.hsd.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CategoryMapper {

    /**
     * 添加分类信息
     *
     * @param category
     * @return
     */
    @Insert("insert into t_category (c_name) values (#{name})")
    int insertCategory(Category category);

    /**
     * 通过ID删除分类信息
     *
     * @param cid
     * @return
     */
    @Delete("delete from t_category where c_id = #{cid}")
    int deleteByID(Integer cid);

    /**
     * 修改分类信息
     *
     * @param category
     * @return
     */
    @Update("update t_category set c_name = #{name} where c_id = #{id}")
    int updateCategory(Category category);

    /**
     * 查询所有分类信息
     *
     * @return
     * @Results 配置映射关系
     * @Select 编写查询语句
     */
    @Results(id = "categoryResults", value = {
            @Result(id = true, property = "id", column = "c_id"),
            @Result(property = "name", column = "c_name")
    })
    @Select("select * from t_category")
    List<Category> selectAll();

    /**
     * 查询指定ID的分类信息
     *
     * @param cid
     * @return
     */
    @ResultMap(value = "categoryResults")
    @Select("select * from t_category where c_id = #{cid}")
    Category selectByID(Integer cid);

    @Results(id = "cbResults", value = {
            @Result(id = true, property = "id", column = "c_id"),
            @Result(property = "name", column = "c_name"),
            @Result(property = "books",column = "c_id",many = @Many(select = "com.hsd.mapper.BookMapper.selectByCID"))
    })
    @Select("select * from t_category where c_id = #{cid}")
    Category selectCBByID(Integer cid);
}
